package edu.webarchitechture.bank.ejb3.services;

import edu.webarchitechture.bank.ejb3.exception.*;
import edu.webarchitechture.bank.ejb3.domain.Tx;

import javax.ejb.Stateless;
import javax.persistence.PersistenceContext;
import java.math.BigDecimal;
import java.util.List;
import java.util.Date;

import org.hibernate.Session;

/**
 * Bean class to implement the Transaction manager.
 *
 * @author Inoka Dissanayaka
 */
@Stateless
public class TransactionManagerBean implements TransactionManager{

    @PersistenceContext
    Session session;

    public void deposit(BigDecimal amount, String description, Long accountId) throws InvalidParameterException, AccountNotFoundException, IllegalAccountTypeException {
        getTransactionManagerDAO().deposit(amount, description, accountId);
    }

    public Tx getTransaction(Long txId) throws TxNotFoundException, InvalidParameterException {
        return getTransactionManagerDAO().getTransaction(txId);
    }

    public List<Tx> getTxsOfAccount(Date startDate, Date endDate, Long accountId) throws InvalidParameterException {
        return  getTransactionManagerDAO().getTxsOfAccount(startDate, endDate, accountId);

    }

    public void makeCharge(BigDecimal amount, String description, Long accountId) throws InvalidParameterException, AccountNotFoundException, IllegalAccountTypeException, InsufficientCreditException {
        getTransactionManagerDAO().makeCharge(amount, description, accountId);


    }

    public void makePayment(BigDecimal amount, String description, Long accountId) throws InvalidParameterException, AccountNotFoundException, IllegalAccountTypeException {
        getTransactionManagerDAO().makePayment(amount, description, accountId);
    }

    public void transferFunds(BigDecimal amount, String description, Long fromAccountId, Long toAccountId) throws InvalidParameterException, AccountNotFoundException, InsufficientFundsException, InsufficientCreditException {
        getTransactionManagerDAO().transferFunds(amount, description, fromAccountId);

    }

    public void withdraw(BigDecimal amount, String description, Long accountId) throws InvalidParameterException, AccountNotFoundException, IllegalAccountTypeException, InsufficientFundsException {
        getTransactionManagerDAO().withdraw(amount, description, accountId);
    }

    public TransactionManagerDAO getTransactionManagerDAO() {
        return new TransactionManagerDAO(session);
    }
}
